;
(function($) {

  jBGallery = {};

  jBGallery.Init = function(options, data) {
    function _init(el) {
      if($(el).data("jbgallery"))
        return;

      var _options = new jBGallery.Options(options, el);

      var _data = new jBGallery.Data(el, options);
      //TEMP? if (_data.length == 0) return;

      $(el).data("jbgallery", new jBGallery.Core(el, _data, _options));
      return $(el).data("jbgallery");
    }

    if(typeof options == "string") {
      if($(this).data("jbgallery"))
        return $(this).eq(0).data("jbgallery")[options](data);
    } else if(typeof data == "boolean") {//return api. break chinability
      return _init($(this).get(0));
    } else {
      return this.each( function(nr) {
        _init(this);
      });
    }
  };
  
  jBGallery.Core = function(el, data, options) {

    var that = el;

    //PRIVATE METHODS
    function _unload() {

      //$("#jbgallery-css").attr("media","javascript-screen");
      $("html, body").removeClass("jbg").removeClass("ie6");

      _engine.target.unbind();
      _engine.target.remove();

      $(that).removeData("jbgallery");

      $(that).unbind();
      $(".jbg-wrap",that).remove();

      //elements - loading, menu, slider, caption
      var _elements = ".jbgs-thumb, .jbgs, .jbgs-opacity, .jbgs-top, .jbgs-top-opacity, .jbg-loading, .jbg-caption, .jbg-menu-opacity, .jbg-menu";//, #jbg-caption-opacity
      $(_elements, el).unbind().find("a").unbind();
      $(_elements, el).remove();

      $(document)
      .unbind('keydown.jbgallery')
      .unbind("mousemove.jbgallery")
      .unbind("click.jbgallery");

      $(window)
      .unbind('resize.jbgallery');
    }

    //MAIN
    new jBGallery.Interface(that, data, options);

    var _engine = new jBGallery.Engine(that, data, options);
    var _keys = new jBGallery.Keys(_engine, options.shortcuts);
    _engine.register(_keys);

    new jBGallery.OnLoadImage(el, _engine, options);

    var _caption = options.caption ? new jBGallery.Caption(that, data, _engine, options) : {};

    if (options.menu) {
      var _menu;
      if (options.menu == 'slider') {
        _menu = new jBGallery.MenuSlider(that, data, _engine, _caption, options);
      } else {
        _menu = new jBGallery.MenuSimple(that, data, _engine, _caption, options);
      }
      _engine.register(_menu);
    }

    //events
    $(that).bind("destroy", _unload);
    $(window).bind("unload", _unload);

    $(that).fadeIn(options.timers.fade, function() {
      options.ready(that);//menuslider - preload images...
    });
    // PUCLIC API:
    return {
      current: _engine.current,
      play   : _engine.play,
      stop   : _engine.stop,
      go     : _engine.go,
      right  : _engine.right,
      left   : _engine.left,
      destroy: _unload,
      //3.0
      length : data.length,
      push   : _engine.push,
      shift  : _engine.shift,
      pop    : _engine.pop,
      unshift: _engine.unshift,
      empty  : _engine.empty
    };
  };
  jBGallery.Adapters = {};

  $.fn.jbgallery = jBGallery.Init;

})(jQuery);